#!/usr/bin/python
# -*- coding: UTF-8 -*-

from time import sleep
from MiniBotControlLib import MiniBotControlLib
from BwSensorLib import BwSensorLib

# 声明控制库对象
mlib = MiniBotControlLib("udp")
# 声明传感器库对象
sensorlib = BwSensorLib(mlib)
# 连接机器人
ip = "10.10.100.254"
mlib.Connect(ip, 8899)

# 超声波传感器测量不同距离，点阵显示不同图案
def main():
   # 点阵显示表情
   # 对应的表情分别是：睁大眼睛、委屈、鄙视、无语、惊喜、眨眼
   displayData = [[0x36,0x49,0x49,0x00,0x3E],[0x63,0x14,0x22,0x00,0x3E],[0x77,0x44,0x00,0x2A,0x14],
               [0x41,0x36,0x00,0x1C,0x22],[0x22,0x77,0x22,0x1C,0x14],[0x20,0x12,0x20,0x08,0x1C]]
   while 1:
      Ultrasonic = sensorlib.readUltrasonicSensor() # 读取超声波传感器数据
   # 5cm~10cm 
      if(Ultrasonic>=5 and Ultrasonic<10):
         sensorlib.setMatrixSensor(displayData[0]) # 显示睁大眼睛
   # 15cm~20cm 
      elif(Ultrasonic>=15 and Ultrasonic<20):
         sensorlib.setMatrixSensor(displayData[1]) # 显示委屈
   # 25cm~30cm 
      elif(Ultrasonic>=25 and Ultrasonic<30):
         sensorlib.setMatrixSensor(displayData[2]) # 显示鄙视
   # 35cm~40cm 
      elif(Ultrasonic>=35 and Ultrasonic<40):
         sensorlib.setMatrixSensor(displayData[3])  # 显示无语  
   # 45cm~50cm 
      elif(Ultrasonic>=45 and Ultrasonic<50):
         sensorlib.setMatrixSensor(displayData[4]) # 显示惊喜
   # 55cm~60cm 
      elif(Ultrasonic>=55 and Ultrasonic<60):
         sensorlib.setMatrixSensor(displayData[5]) # 显示眨眼

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print(" Program closed! ")
    pass


